package lancooPaperContent;

import android.content.Context;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.widget.ImageButton;
import android.widget.SeekBar;
import android.widget.TextView;

import com.example.lancooznbk.R;
import com.example.lancooznbk.presenter.PaperDef;
import com.example.lancooznbk.view.widget.VoicePlayer;

import org.apache.commons.lang3.StringEscapeUtils;

import static testPaperCTCS.GuessTestingPaperActivity.InforGuessTesting;
import static testPaperFavoritesPage.FavoritesPagePaperActivity.InforFavoritesPage;
import static testPaperJBFX.BasicReviewPaperActivity.InforBasicReview;
import static testPaperNDFX.DifficultReviewPaperActivity.InforDifficultReview;
import static testPaperWrongHistory.HistoryWrongQuesPaperActivity.InforHistoryWrongQues;
import static testPaperWrong.WrongQuesPaperActivity.InforWrongQues;

/**
 * Created by Administrator on 2017/4/21.
 */
public class ContentTltkAdapter extends WrapBaseAdapter

{

    private LayoutInflater mInflater;
    //当前试题数量
    private int ItemSum;
    //当前试题ID
    private int PaperID;
    private int ModelFrom;//试卷来源


    private Context mContext;

    private ViewHolder holder = null;
    public final class ViewHolder {
        WebView WvTheme;
        ImageButton BtnPlayPause;
        TextView TvAnswerTime;
        SeekBar SbMusic;
        private ViewHolder(View convertView) {
            WvTheme = (WebView) convertView.findViewById(R.id.wv_theme);
            BtnPlayPause = (ImageButton) convertView.findViewById(R.id.btn_start);
            TvAnswerTime = (TextView) convertView.findViewById(R.id.tv_answerTime);
            SbMusic = (SeekBar) convertView.findViewById(R.id.music_progress);
        }
    }

    public ContentTltkAdapter(Context context, int itemSum, int itemID, int modelFrom) {
        mInflater = LayoutInflater.from(context);
        this.mContext = context;
        this.ItemSum = itemSum;
        this.PaperID = itemID;
        this.ModelFrom= modelFrom;
    }

    @Override
    public int getCount() {
        return ItemSum;
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.adapter_content_tltk_item, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        /*
      * 获取试题信息
      * */
        getQuesInfor();

        /*
        * 获取题目信息
        * */
        showQuesTheme();

        return convertView;
    }

    private String theme;//获取大题题干信息
    private int state;//试题提交状态
    private String quesAudio;//音频路径

    /*
  * 获取试题信息
  * */
    private void getQuesInfor(){

        if(ModelFrom==PaperDef.FROM_BASE_REVIEW) {
            state = InforBasicReview.getState();
            theme = InforBasicReview.getPaperContent().get(PaperID).getQueList().get(0).getItemContent();
            quesAudio = InforBasicReview.getPaperContent().get(PaperID).getQuesAudio().get(0).getURL();
        }else if(ModelFrom==PaperDef.FROM_DIFFERENT_REVIEW){
            theme = InforDifficultReview.getQueList().get(0).getItemContent();
        }else if(ModelFrom==PaperDef.FROM_GUESS_REVIEW){
            state = InforGuessTesting.getState();
            theme = InforGuessTesting.getPaperContent().get(PaperID).getQueList().get(0).getItemContent();
            quesAudio = InforGuessTesting.getPaperContent().get(PaperID).getQuesAudio().get(0).getURL();
        }else if(ModelFrom== PaperDef.FROM_BASE_REVIEW_WRONG||ModelFrom==PaperDef.FROM_DIFFERENT_REVIEW_WRONG){
            state = InforWrongQues.get(PaperID).getState();
            theme =  InforWrongQues.get(PaperID).getQueList().get(0).getItemContent();
            quesAudio = InforWrongQues.get(PaperID).getQuesAudio().get(0).getURL();
        }else if(ModelFrom==PaperDef.FROM_BASE_REVIEW_WRONG_History||ModelFrom==PaperDef.FROM_DIFFERENT_REVIEW_WRONG_History){
            state = InforHistoryWrongQues.get(PaperID).getState();
            theme =  InforHistoryWrongQues.get(PaperID).getQueList().get(0).getItemContent();
            quesAudio = InforHistoryWrongQues.get(PaperID).getQuesAudio().get(0).getURL();
        }else if(ModelFrom==PaperDef.FROM_FAVOURITE_REVIEW){
            state = InforFavoritesPage.getState();
            theme = InforFavoritesPage.getQueList().get(0).getItemContent();
            quesAudio = InforFavoritesPage.getQuesAudio().get(0).getURL();
        }

        mVoicePlayer = new VoicePlayer(holder.SbMusic,quesAudio,holder.TvAnswerTime,holder.BtnPlayPause,true);

    }

    /*
* 获取题目题干信息
* */
    Handler htmlHandlerItemContent = new Handler();
    private void showQuesTheme(){
        theme = StringEscapeUtils.unescapeXml(theme);
        holder.WvTheme.loadDataWithBaseURL("about:blank",theme,"text/html","utf-8",null);
    }

}